﻿
using UnityEngine;
using UnityEngine.UI;

[AddComponentMenu("BToolkit/CText")]
[RequireComponent(typeof(CTextEffect))]
public class CText : Text
{
    [System.Serializable]
    public class Outline
    {
        public Color color = new Color(0.6f, 0.2f, 0);
        public float size = 5;
    }
    [System.Serializable]
    public class Shadow
    {
        public Color color = new Color(0, 0, 0, 0.5f);
        public Vector2 offset = Vector2.zero;
    }

    //渐变
    public enum GradientColor { None, Horizontal, Vertical }
    public GradientColor gradientColor = GradientColor.None;
    public Color32 begin = Color.white;
    public Color32 end = new Color(1, 1, 0);
    //描边
    public Outline[] outlines = new Outline[0];
    //阴影
    public Shadow[] shadows = new Shadow[0];

    protected override void Start()
    {
        base.Start();
        CTextEffect textEffect = GetComponent<CTextEffect>();
        textEffect.enabled = true;
        textEffect.Refresh();
    }

#if UNITY_EDITOR
    [UnityEditor.MenuItem("BToolkit/创建 CText")]
    [UnityEditor.MenuItem("GameObject/BToolkit/创建 CText")]
    static void Create()
    {
        GameObject go = new GameObject("Text");
        go.layer = LayerMask.NameToLayer("UI");
        go.transform.SetParent(UnityEditor.Selection.activeGameObject.transform, false);
        CText cText = go.AddComponent<CText>();
        cText.alignment = TextAnchor.MiddleCenter;
        cText.horizontalOverflow = HorizontalWrapMode.Overflow;
        cText.verticalOverflow = VerticalWrapMode.Overflow;
        cText.rectTransform.sizeDelta = Vector2.zero;
        UnityEditor.EditorUtility.SetDirty(go);
    }


    [UnityEditor.MenuItem("GameObject/BToolkit/Text 转 CText", false, -200)]
    public static void ChangeToCText()
    {
        for (int i = 0; i < UnityEditor.Selection.objects.Length; i++)
        {
            GameObject go = UnityEditor.Selection.objects[i] as GameObject;
            string content = "";
            Font font = null;
            FontStyle fontStyle = FontStyle.Normal;
            int fontSize = 0;
            float lineSpace = 0;
            bool supportRichText = true;
            TextAnchor alignment = TextAnchor.MiddleCenter;
            HorizontalWrapMode horizontalOverflow = HorizontalWrapMode.Overflow;
            VerticalWrapMode verticalOverflow = VerticalWrapMode.Overflow;
            Color color = Color.white;
            bool raycastTarget = true;
            Image image = go.GetComponent<Image>();
            if (image)
            {
                DestroyImmediate(image);
            }
            Text text = go.GetComponent<Text>();
            if (text)
            {
                content = text.text;
                font = text.font;
                fontStyle = text.fontStyle;
                fontSize = text.fontSize;
                lineSpace = text.lineSpacing;
                supportRichText = text.supportRichText;
                alignment = text.alignment;
                horizontalOverflow = text.horizontalOverflow;
                verticalOverflow = text.verticalOverflow;
                color = text.color;
                raycastTarget = text.raycastTarget;
                DestroyImmediate(text);
            }
            CText cText = go.GetComponent<CText>();
            if (!cText) cText = go.AddComponent<CText>();
            cText.text = content;
            if (font) cText.font = font;
            if (fontSize > 0) cText.fontSize = fontSize;
            if (lineSpace > 0) cText.lineSpacing = lineSpace;
            cText.fontStyle = fontStyle;
            cText.supportRichText = supportRichText;
            cText.alignment = alignment;
            cText.horizontalOverflow = horizontalOverflow;
            cText.verticalOverflow = verticalOverflow;
            cText.color = color;
            cText.raycastTarget = raycastTarget;
        }
    }

    [UnityEditor.MenuItem("GameObject/BToolkit/Text 转 Overflow", false, -200)]
    public static void ChangeToOverflow()
    {
        for (int i = 0; i < UnityEditor.Selection.objects.Length; i++)
        {
            GameObject go = UnityEditor.Selection.objects[i] as GameObject;
            Text text = go.GetComponent<Text>();
            if (text)
            {
                text.rectTransform.sizeDelta = Vector2.zero;
                text.alignment = TextAnchor.MiddleCenter;
                text.horizontalOverflow = HorizontalWrapMode.Overflow;
                text.verticalOverflow = VerticalWrapMode.Overflow;
                text.color = Color.white;
            }
            CText cText = go.GetComponent<CText>();
            if (cText)
            {
                cText.rectTransform.sizeDelta = Vector2.zero;
                cText.alignment = TextAnchor.MiddleCenter;
                cText.horizontalOverflow = HorizontalWrapMode.Overflow;
                cText.verticalOverflow = VerticalWrapMode.Overflow;
                cText.color = Color.white;
            }
        }
    }
#endif
}